package modellayer;

import java.util.ArrayList;

/**
 * This is a container-class for simple
 * non-containers classes implementing the
 * <code>DiscountSubTotal</code> interface.
 * Useful for objects that needs to know which
 * discounts they have and how to calculate
 * them given only a price and an amount.
 * 
 * @author (ucn-1semprojekt-dm78-gruppe1)
 *
 */
@SuppressWarnings("serial")
public class Discounts extends FileIt implements DiscountSubTotal
{
	private ArrayList<DiscountSubTotal> discountList;

	/**
	 * @param fileName the place to save the contents of this
	 * container
	 */
	public Discounts(String fileName)
	{		
		super(fileName);
		discountList = new ArrayList<DiscountSubTotal>();
		setFileItObject(discountList);
	}

	/**
	 * This method is called from <code>super</code> to
	 * ensure correct type-cast after a <code>super.load()</code>
	 * 
	 * @param loaded to be cast into an ArrayList
	 * @see modellayer.FileIt#updateFileItObject(java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void updateFileItObject(Object loaded)
	{
		// TODO Auto-generated method stub
		discountList = (ArrayList<DiscountSubTotal>)loaded;
	}

	/**
	 * One intent is to pass this to a control-view GUI
	 * to do some manipulation by user
	 * @return the discountList
	 */
	public ArrayList<DiscountSubTotal> getDiscountList()
	{
		return discountList;
	}

	/**
	 * One intent is to migrate data from an other system
	 * @param discountList
	 *          the discountList to set
	 */
	public void setDiscountList(ArrayList<DiscountSubTotal> discountList)
	{
		this.discountList = discountList;
	}

	/**
	 * Run through all those different discounts
	 * to lower the price. Then multiply with <code>amount</code>
	 * to make the subtotal.	
	 * @param normPrice the price to be discounted (cut off)
	 * @param amount the amount or pieces to be bought.
	 * @see modellayer.DiscountSubTotal#subTotal(double, int)
	 */
	@Override
	public double subTotal(double normPrice, int amount)
	{
		// TODO Auto-generated method stub
		double price = normPrice;

		for (DiscountSubTotal discount:discountList)
		{
			price = discount.subTotal(price,amount);
		}
		return price * amount;
	}

	/**
	 * @return a string representation of this object
	 */
	public String toString()
	{
		StringBuilder s = new StringBuilder();
		for (DiscountSubTotal discount:discountList)
		{
			s.append(discount + "\n");
		}
		return s.toString();
	}
	
	
}
